package com.zhang.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.zhang.springcloud.block.BlockHandler;
import com.zhang.springcloud.entities.CommonResult;
import com.zhang.springcloud.entities.Payment;
import com.zhang.springcloud.fallback.Fallback;
import com.zhang.springcloud.feign.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${service-url.nacos-user-service}")
    private String Service_Name;
    @GetMapping("/consumer/nacos/{id}")
    @SentinelResource(value = "fallback",fallbackClass = Fallback.class,fallback = "fallbackMehod",
            blockHandlerClass = BlockHandler.class,blockHandler = "BlockHandlerMethod")
    public CommonResult<Payment> get(@PathVariable("id") Integer id){
        CommonResult<Payment> cr = restTemplate.getForObject(Service_Name+"/paymentSQL/"+id,CommonResult.class);
        if(id == 4){
            throw new IllegalArgumentException("IllegalArgumentException非法数据");
        }else if( cr.getData() == null ){
            throw new NullPointerException("NullPointerException非法数据");
        }
        return cr;
    }
    @GetMapping("/consumer/nacos1/{id}")
    public String getFeign1(@PathVariable("id") Integer id){
        return "String";
    }
    @Autowired
    ConsumerService cs;
    @GetMapping("/paymentSQL/consumer/{id}")
    public CommonResult<Payment> getFeign(@PathVariable("id") Integer id){
        return cs.getFeign(id);
    }
}
